package com.jiajia.study.socket;


import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * 网络编程
 * @author : zjj
 * @date : 2021-04-19 12:27
 */
public class Client {


    public static void main(String[] args) throws Exception{
        // ServerTest();

        // ClientTest();

        // FileServerTest();

        // FileClientTest();

        //CommunicationServerTest();

        CommunicationClientTest();
    }

    /**
     * 客户端
     */
    private static void ClientTest(){
        Socket socket = null;
        OutputStream outputStream = null;

        try{
            // 创建socket对象，指明服务器端的ip和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            // 创建网络连接，和server端进行网络连接
            socket = new Socket(inet,8888);
            // 获取输出流,用户输出数据
            outputStream = socket.getOutputStream();
            // 写出数据的操作
            outputStream.write("你好,我是客户端".getBytes());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(outputStream != null){
                    outputStream.close();
                }
                if(socket != null){
                    socket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }


    }

    /**
     * 服务端
     */
    private static void ServerTest(){

        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;

        try {

            serverSocket = new ServerSocket(8888);
            System.out.println("服务端启动，等待接收客户端的收据");
            // 这里会等待客户端来连接
            socket = serverSocket.accept();

            inputStream = socket.getInputStream();

            byteArrayOutputStream = new ByteArrayOutputStream();

            byte[] buffer = new byte[10];
            int len;
            while((len = inputStream.read(buffer)) != -1){
                byteArrayOutputStream.write(buffer,0,len);
            }

            System.out.println(byteArrayOutputStream.toString());

            System.out.println("收到了来自于：" + socket.getInetAddress().getHostAddress() + "的数据");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{

                if(socket != null){
                    socket.close();
                }
                if(byteArrayOutputStream != null){
                    byteArrayOutputStream.close();
                }
                if(inputStream != null){
                    inputStream.close();
                }
                if(serverSocket != null){
                    serverSocket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * server端，接收文件
     */
    private static void FileServerTest()throws Exception{

        ServerSocket serverSocket = new ServerSocket(8888);

        Socket socket = serverSocket.accept();

        InputStream inputStream = socket.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream(new File("socket_beauty.jpg"));

        byte[] buffer = new byte[10];
        int len;
        while((len = inputStream.read(buffer)) != -1){
            fileOutputStream.write(buffer,0,len);
        }

        // 关闭流
        fileOutputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();

    }

    /**
     * 文件网络传输 client端
     */
    private static void FileClientTest()throws Exception{
        // 建立网络连接
        Socket socket = new Socket("127.0.0.1",8888);

        OutputStream outputStream = socket.getOutputStream();

        FileInputStream fileInputStream = new FileInputStream(new File("beauty.jpg"));

        byte[] buffer = new byte[1024];
        int len;
        while((len = fileInputStream.read(buffer)) != -1){
            outputStream.write(buffer,0,len);
        }

        //关闭流
        fileInputStream.close();
        outputStream.close();
        socket.close();


    }

    /**
     * 通信客户端
     */
    private static void CommunicationServerTest()throws Exception{

        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept();

        InputStream inputStream = socket.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream(new File("socket_beauty.mp4"));

        byte[] buffer = new byte[1024];

        int len;

        while((len = inputStream.read(buffer)) != -1){
            fileOutputStream.write(buffer,0,len);
        }

        System.out.println("图片传输完成");

        // 返回数据到客户端
        OutputStream outputStream = socket.getOutputStream();

        outputStream.write("视频接收完成，nice".getBytes());


        // 关闭资源
        outputStream.close();
        fileOutputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();


    }

    /**
     * 通信客户端测试
     */
    private static void CommunicationClientTest()throws Exception{
        // 发送文件到服务器中
        Socket socket = new Socket("127.0.0.1",8888);
        OutputStream outputStream = socket.getOutputStream();

        FileInputStream fileInputStream = new FileInputStream(new File("beauty.mp4"));

        byte[] buffer = new byte[1024];
        int len ;
        while ((len = fileInputStream.read(buffer)) != -1){
            outputStream.write(buffer,0,len);
        }

        //关闭数据的输出,这里server端就会一直等待继续接收数据，而客户端一直等待接收server端的数据
        // 就会卡死在这里
        socket.shutdownOutput();

        // 从服务端接收数据
        InputStream inputStream = socket.getInputStream();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bufferReceive = new byte[20];
        int lenReceive;
        while((lenReceive = inputStream.read(bufferReceive)) != -1){
            byteArrayOutputStream.write(bufferReceive,0,lenReceive);
        }

        System.out.println(byteArrayOutputStream.toString());

        // 关闭资源
        byteArrayOutputStream.close();
        inputStream.close();
        fileInputStream.close();
        outputStream.close();
        socket.close();

    }

}
